<?php
/**
 * Created by PhpStorm.
 * User: hang
 * Date: 2021/12/14
 * Time: 23:50
 */
include_once '../tools/DbTools.php';
include_once '../user/common.php';
$is_login = getLoginStatus();
if (!$is_login){
    echo '尚未登录，<a href="../user/loginUi.php">去登录</a>';
}else{
    session_start();
    $payeeCard = $_POST['payeeCard'];
    $transfer_Money = $_POST['transfer_Money'];
    $token = $_COOKIE['Token'];
    $userInfo = $_SESSION[$token];
    $CardId = $userInfo['CardId'];
    DbTools::initDb();
    $sql="select CardMoney from bankcard where CardId='$CardId'";
    $result = DbTools::select($sql);
    $money = $result[0]['CardMoney'];
    $sql = "select CardNo from bankcard where CardNo='$payeeCard'";
    $result = DbTools::select($sql);
    if (empty($result[0]['CardNo'])){
        echo '余额剩余：'.$money.'<br/>';
        echo '收款卡号输入错误！<a href="transfer.php">重新输入</a>';
        die;
    }
    if ($transfer_Money>0){
        if ($transfer_Money>$money){
            echo '余额不足，<a href="transfer.php">重新输入</a>';
            die;
        }else{
            $sql = "update bankcard set CardMoney=CardMoney-$transfer_Money where CardId=$CardId";
            DbTools::noSelect($sql);

            $sql="update bankcard set CardMoney=CardMoney+$transfer_Money where CardNo=$payeeCard";
            DbTools::noSelect($sql);
            $money = $money-$transfer_Money;
            echo '转账成功！余额剩余：'.$money.'<br/>';
            echo '<a href="transfer.php">继续转账</a>'.'<br/>';
            echo '<a href="../user/userInfo.php">用户中心</a>';
            DbTools::close();
        }
    }else{
        echo '余额剩余：'.$money.'<br/>';
        echo '转账金额输入错误！<a href="transfer.php"> 重新输入</a>';

        die;
    }

}